package com.billy.ohos.loadingstatusview.wrapability.adapter;

import com.billy.ohos.loading.Gloading;
import com.billy.ohos.loadingstatusview.ResourceTable;
import com.billy.ohos.loadingstatusview.wrapability.adapter.view.GlobalLoadingStatusView;
import com.billy.ohos.loadingstatusview.wrapability.adapter.view.anim.LVFinePoiStar;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import static com.billy.ohos.loading.Gloading.STATUS_LOADING;

/**
 * demo:
 * when status == STATUS_LOADING use another UI
 * otherwise, use the same UI as the global status view
 *
 * @author billy.qi
 * @since 19/3/19 23:20
 */
public class SpecialAdapter implements Gloading.Adapter {

    @Override
    public Component getComponent(Gloading.Holder holder, Component convertView, int status) {
        if (status == STATUS_LOADING) {
            // only loading UI special
            SpecialLoadingStatusView view;
            if (convertView == null || !(convertView instanceof SpecialLoadingStatusView)) {
                view = new SpecialLoadingStatusView(holder.getContext());
                convertView = view;
            } else {
                view = (SpecialLoadingStatusView) convertView;
            }
            view.start();
        } else {
            // other status use global UI
            GlobalLoadingStatusView view;
            if (convertView == null || !(convertView instanceof GlobalLoadingStatusView)) {
                view = new GlobalLoadingStatusView(holder.getContext(), holder.getRetryTask());
                convertView = view;
            } else {
                view = (GlobalLoadingStatusView) convertView;
            }
            view.setStatus(status);
        }
        return convertView;
    }

    /**
     * special loading status view for only one activity usage
     *
     * @author billy.qi
     * @since 19/3/19 23:12
     */
    class SpecialLoadingStatusView extends DependentLayout implements Component.BindStateChangedListener {

        private final LVFinePoiStar lvFinePoiStar;

        public SpecialLoadingStatusView(Context context) {
            super(context);
            setGravity(LayoutAlignment.CENTER);
            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(0xCCCCCCCC));
            setBackground(element);

            Component parse = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_view_special_loading, null, false);
            ShapeElement element1 = new ShapeElement();
            element1.setRgbColor(new RgbColor(0x0000004F));
            parse.setBackground(element1);
            addComponent(parse);

            lvFinePoiStar = (LVFinePoiStar) findComponentById(ResourceTable.Id_loading_anim);
            lvFinePoiStar.setViewColor(Color.WHITE.getValue());
            lvFinePoiStar.setCircleColor(Color.YELLOW.getValue());
            lvFinePoiStar.setDrawPath(true);
        }

        @Override
        public void onComponentUnboundFromWindow(Component component) {
            lvFinePoiStar.stopAnim();
        }

        public void start() {
            lvFinePoiStar.startAnim(2000);
        }

        @Override
        public void onComponentBoundToWindow(Component component) {

        }

    }
}
